<?php
namespace addon\cms\validate;
use app\common\validate\ValidateBase;
class Fields extends ValidateBase
{
    // 验证规则
    protected $rule = [
        'type'                  => 'require|checkType',
        'title'                 => 'require',
        'name'                  => 'require|regex:^[a-z][a-z0-9_]{2,63}|length:3,64'
    ];

    // 验证提示
    protected $message = [
        'type.require'         => '字段类型必须',
        'title.require'        => '字段标题必须',
        'name.require'         => '字段名必须',
        'name.regex'           => '字段名规则不匹配',
        'name.length'          => '字段名长度3-64位',
    ];

    // 应用场景
    protected $scene = [
        'add'  =>  ['type','title','name'],
    ];
    protected function checkType($value){
        $keys = config("config.fields_type");
        foreach ($keys as $k => $v) {
            if($value==$v['key']){
                return true;
            }
        }
        return "字段类型非法";
    }
}